<?php

namespace App\Http\Controllers;

use App\Http\Requests\BookRequest;
use App\Models\Book;
use App\Models\FsUser;
use App\Transformers\FsUserTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class BookController extends Controller
{
    /**
     * @api {post} /api/book 笔记添加
     * @apiGroup  Book
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {array} ids  会员id的数组
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     */
    public function store(BookRequest $bookRequest, Book $book)
    {
        $res = $book->where('user_id',Auth::id())->first();
        if($res){
            $res = $res->toArray();
            $data['fs_user_ids'] = json_encode(array_unique(array_merge(json_decode($res['fs_user_ids'],true),$bookRequest->ids)));
            $book->where('user_id',Auth::id())->update($data);
        }else{
            $data['fs_user_ids'] = json_encode($bookRequest->ids);
            $data['user_id'] = Auth::id();
            $book->create($data);
        }
        return response()->json(['message' => '添加成功']);
    }

    /**
     * @api {get} /api/book?page=1&limit=1&include=fsUserPhones,fsUserRemarks,fsUserHasIm 笔记列表
     * @apiGroup  Book
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {string} search  值
     * @apiParam {array} ids  筛选的会员id
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *           {
     *               "id": 51,
     *               "username": "Jamarcus",
     *               "agent": "上海平台1",
     *               "register_time": "2017-05-06 07:46:10",
     *               "balance": "10000.00",
     *               "last_login_time": "2017-05-06 07:46:10",
     *               "last_charge_time": "2019-01-15 07:46:10",
     *               "last_refund_time": "2017-05-06 07:46:10",
     *               "ip": "",
     *               "status": "",
     *               "lev": "",
     *               "true_name": "Mrs. Jade Thiel V",
     *               "true_id": "",
     *               "phone_num": "18911392274",
     *               "qq": "",
     *               "mail": "",
     *               "province": "",
     *               "city": "",
     *               "bank_name": "",
     *               "bank_num": "",
     *               "remark": "",
     *               "site_id": null,
     *               "deposit_count": "73.00",
     *               "deposit_max": "67566.00",
     *               "deposit_total": "557230.00",
     *               "withdrawal_count": "2.00",
     *               "withdrawal_total": "810.00",
     *               "multiple": "0.0000",
     *               "profit": null,
     *               "call_status": ""
     *               }
     *           ],
     *           "meta": {
     *               "pagination": {
     *                       "total": 2,
     *                       "count": 1,
     *                       "per_page": 1,
     *                       "current_page": 1,
     *                       "total_pages": 2,
     *                       "links": {
     *                       "previous": null,
     *                       "next": "http://dianyun2.com/api/book?page=2"
     *                   }
     *               }
     *           }
     *       }
     */
    public function index(Request $request, Book $book,FsUser $fsUser)
    {
        $authId = Auth::id();
        $res = $book->where('user_id', $authId)->first();
        if($res){
            if(json_decode($res->fs_user_ids, true)){
                $ids = json_decode($res->fs_user_ids, true);
                $res = $fsUser->bookSearch($request,$ids);
                return $this->response->paginator($res, new FsUserTransformer());
            }
        }else{
            return $this->response->array(['data'=>[]]);
        }
    }

    /**
     * @api {delete} /api/book 笔记删除
     * @apiGroup  Book
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {array} ids  会员id的数组
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "message": "删除成功"
     *       }
     */
    public function destroy(BookRequest $bookRequest,Book $book){
        $res = $book->where('user_id',Auth::id())->first();
        $delete_id = $bookRequest->ids;
        $fsUserids = json_decode($res->fs_user_ids,true);
        $new_ids = array_diff($fsUserids,$delete_id);
        $data['fs_user_ids'] = json_encode($new_ids);
        $res->update($data);
        return response()->json(['message' => '删除成功']);
    }
}
